home *** CD-ROM | disk | FTP | other *** search
- /*
- * Module: RunImage.c
- * Creation: 29th May, 1993.
- * Last modified: 26th June, 1993.
- *
- * (c) Copyright Neil Hoggarth, 1993.
- *
- * This text file is ANSI C source code.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 1, or (at your option)
- * any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
-
- #include "baricon.h" /* Icon bar stuff from RISC_OSlib */
- #include "dbox.h" /* Dialogue box stuff from RISC_OSlib */
- #include "event.h" /* WIMP events from RISC_OSlib */
- #include "menu.h" /* Menu handling from RISC_OSlib */
- #include "res.h" /* Resources from you know where */
- #include "resspr.h" /* Something to do with sprites I think */
- #include "saveas.h" /* File handling by icon drags from RISC_OSlib */
- #include "template.h" /* Template routines from RISC_OSlib */
- #include "visdelay.h" /* Hourglass functions from RISC_OSlib */
- #include "werr.h" /* RISC_OSlib WIMP error box */
- #include "win.h" /* WIMP stuff from RISC_OSlib */
- #include "wimpt.h" /* ditto */
- #include "xferrecv.h" /* File handling by icon drags from RISC_OSlib */
-
- /*
- * A suggestion to Acorn for the next compiler release. The above is
- * ridiculous! Why can't we just have a "riscos.h" header and be done
- * with it? It would save me some typing anyway.
- */
-
- /******************************** Constants *****************************/
-
- enum {IM_INFO=1,IM_OPTIONS,IM_SAVE,IM_QUIT}; /* Icon bar menu items */
- #define SPRITE_FILETYPE 0xff9
- #define JFIF_FILETYPE 0xc85
-
- /***************************** Global Variables *************************/
-
- static int finished=FALSE;
- static char option_q[12];
- static char option_mode[12];
-
- /*************************** Function Definitions ***********************/
-
- static menu MenuMaker(void *handle)
- /*
- * Builds the icon bar menu.
- */
- {
- menu temp;
-
- handle=handle; /* Supresses a compiler warning. */
-
- temp=menu_new("JFIF",">Info,>Options,Save Options,Quit");
- return(temp);
- }
-
- static void MenuProc(void *handle, char *hit)
- /*
- * Handles choices from the icon bar menu.
- */
- {
- dbox i;
- FILE *stream;
-
- handle=handle; /* Supresses a compiler warning */
-
- switch(hit[0])
- {
- case IM_INFO:
- i=dbox_new("progInfo");
- dbox_showstatic(i);
- dbox_fillin(i);
- dbox_dispose(&i);
- break;
- case IM_OPTIONS:
- i=dbox_new("options");
- dbox_setfield(i,1,option_q);
- dbox_setfield(i,3,option_mode);
- dbox_showstatic(i);
- dbox_fillin(i);
- dbox_getfield(i,1,option_q,12);
- dbox_getfield(i,3,option_mode,12);
- dbox_dispose(&i);
- break;
- case IM_SAVE:
- stream=fopen("<jfif$dir>.options","w");
- if (stream==NULL)
- {
- werr(FALSE,"Can't open options file for writting. Check that the disc or filesystem is not write protected.");
- }
- else
- {
- fprintf(stream,"%s\n%s\n",option_q,option_mode);
- fclose(stream);
- }
- break;
- case IM_QUIT:
- finished=TRUE;
- break;
- }
- return;
- }
-
- static BOOL sprite_to_jfif(char *output_file,void *handle)
- {
- char *input_file;
- char cmd[256];
-
- input_file=handle;
- sprintf(cmd,"<changefsi$dir>.changefsi %s <wimp$scrapdir>.pbm p6 -nomode",input_file);
- wimp_starttask(cmd);
- sprintf(cmd,"<changefsi$dir>.cjpeg -Q %s <wimp$scrapdir>.pbm %s",option_q,output_file);
- wimp_starttask(cmd);
- sprintf(cmd,"delete <wimp$scrapdir>.pbm");
- wimp_starttask(cmd);
- sprintf(cmd,"settype %s %x",output_file,JFIF_FILETYPE);
- wimp_starttask(cmd);
- return(TRUE);
- }
-
- static BOOL jfif_to_sprite(char *output_file,void *handle)
- {
- char *input_file;
- char cmd[256];
-
- input_file=handle;
- sprintf(cmd,"<changefsi$dir>.djpeg %s <wimp$scrapdir>.pbm",input_file);
- wimp_starttask(cmd);
- sprintf(cmd,"<changefsi$dir>.changefsi <wimp$scrapdir>.pbm %s %s -nomode",output_file,option_mode);
- wimp_starttask(cmd);
- sprintf(cmd,"delete <wimp$scrapdir>.pbm");
- wimp_starttask(cmd);
- return(TRUE);
- }
-
- static void EventHandler(wimp_eventstr *e,void *handle)
- /*
- * This is the event handler for the sprite on the icon bar.
- */
- {
- char *filename;
- static char *input_file;
- int filetype;
-
- handle=handle; /* This line prevents a compiler warning. */
-
- switch (e->e)
- {
- case wimp_EREDRAW:
- (void)wimpt_checkmode();
- break;
- case wimp_ESEND:
- case wimp_ESENDWANTACK:
- if ( e->data.msg.hdr.action==wimp_MDATALOAD )
- {
- filetype=xferrecv_checkinsert(&filename);
- input_file=(char *)malloc(sizeof(char)*(strlen(filename)+1));
- strcpy(input_file,filename);
- if (filetype==SPRITE_FILETYPE)
- saveas(JFIF_FILETYPE,"JPEG",0,sprite_to_jfif,0,0,input_file);
- else
- saveas(SPRITE_FILETYPE,"sprite",0,jfif_to_sprite,0,0,input_file);
- free(input_file);
- }
- break;
- default:
- /*
- * It's not an event that we wish to respond to,
- * therefore we ignore it.
- */
- break;
- }
- }
-
- static void Init(void)
- /*
- * Declare the program as a WIMP task and initialise all the RISC_OSlib
- * stuff.
- */
- {
- wimpt_init("JFIF");
- res_init("JFIF");
- resspr_init();
- template_init();
- dbox_init();
- visdelay_init();
-
- return;
- }
-
- static void LoadOptions(void)
- {
- FILE *stream;
-
- stream=fopen("<jfif$dir>.options","r");
- if (stream==NULL)
- {
- werr(FALSE,"Can't open options file for reading - using default values");
- strcpy(option_q,"75");
- strcpy(option_mode,"21");
- }
- else
- {
- fscanf(stream,"%s",option_q);
- fscanf(stream,"%s",option_mode);
- fclose(stream);
- }
- return;
- }
-
- /****************************** Main Program ****************************/
-
- int main(void)
- {
- Init();
-
- baricon("!jfif", (int)resspr_area(), 0);
- win_register_event_handler(win_ICONBAR, EventHandler, 0);
- event_attachmenumaker(win_ICONBAR, MenuMaker, MenuProc, 0);
- win_claim_unknown_events(win_ICONBAR);
- xfersend_close_on_xfer(TRUE,win_ICONBAR);
-
- LoadOptions();
-
- while(!finished)
- event_process();
- }
-